﻿using System;
using System.Collections.Generic;
using System.Windows;
using Project5188.BUS;

namespace Project5188.Client
{
    /// <summary>
    /// Interaction logic for PagingControl.xaml
    /// </summary>
    public partial class PagingControl
    {
        public event EventHandler DataChanged;

        public dynamic Paging { get; set; }

        public void InvokeDataChanged(EventArgs e)
        {
            var handler = DataChanged;
            if (handler != null) handler(this, e);
        }

        public void SetDataSource<T>(List<T> dataSource, int pageNo, int recordCount)
        {
            Paging = new Paging<T>(dataSource, pageNo, recordCount);
        }

        public PagingControl()
        {
            InitializeComponent();

        }

        private void BtnFirstClick(object sender, RoutedEventArgs e)
        {
            if (Paging != null && Paging.PageNo != 1)
            {
                Paging.PageNo = 1;
                lblPageNumber.Content = 1;
                InvokeDataChanged(e);
            }
        }

        private void BtnPrevClick(object sender, RoutedEventArgs e)
        {
            if (Paging != null && Paging.PageNo > 1)
            {
                Paging.PageNo -= 1;
                lblPageNumber.Content = Paging.PageNo;
                InvokeDataChanged(e);
            }
        }

        private void BtnNextClick(object sender, RoutedEventArgs e)
        {
            if (Paging != null && Paging.PageNo * Paging.RecordCount < Paging.DataList.Count)
            {
                Paging.PageNo += 1;
                lblPageNumber.Content = Paging.PageNo;
                InvokeDataChanged(e);
            }
        }

        private void BtnLastClick(object sender, RoutedEventArgs e)
        {
            if (Paging != null && Paging.PageNo * Paging.RecordCount < Paging.DataList.Count)
            {
                double value = Convert.ToDouble(Paging.DataList.Count)/Convert.ToDouble(Paging.RecordCount);
                Paging.PageNo = Convert.ToInt32(Math.Ceiling(value));
                lblPageNumber.Content = Paging.PageNo;
                InvokeDataChanged(e);
            }
        }
    }
}
